home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / js / jsopcode.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  13KB  |  302 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Mozilla Communicator client code, released
  17.  * March 31, 1998.
  18.  *
  19.  * The Initial Developer of the Original Code is
  20.  * Netscape Communications Corporation.
  21.  * Portions created by the Initial Developer are Copyright (C) 1998
  22.  * the Initial Developer. All Rights Reserved.
  23.  *
  24.  * Contributor(s):
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  28.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. #ifndef jsopcode_h___
  41. #define jsopcode_h___
  42. /*
  43.  * JS bytecode definitions.
  44.  */
  45. #include <stddef.h>
  46. #include "jsprvtd.h"
  47. #include "jspubtd.h"
  48.  
  49. JS_BEGIN_EXTERN_C
  50.  
  51. /*
  52.  * JS operation bytecodes.
  53.  */
  54. typedef enum JSOp {
  55. #define OPDEF(op,val,name,token,length,nuses,ndefs,prec,format) \
  56.     op = val,
  57. #include "jsopcode.tbl"
  58. #undef OPDEF
  59.     JSOP_LIMIT
  60. } JSOp;
  61.  
  62. /*
  63.  * JS bytecode formats.
  64.  */
  65. #define JOF_BYTE          0       /* single bytecode, no immediates */
  66. #define JOF_JUMP          1       /* signed 16-bit jump offset immediate */
  67. #define JOF_CONST         2       /* unsigned 16-bit constant pool index */
  68. #define JOF_UINT16        3       /* unsigned 16-bit immediate operand */
  69. #define JOF_TABLESWITCH   4       /* table switch */
  70. #define JOF_LOOKUPSWITCH  5       /* lookup switch */
  71. #define JOF_QARG          6       /* quickened get/set function argument ops */
  72. #define JOF_QVAR          7       /* quickened get/set local variable ops */
  73. #define JOF_INDEXCONST    8       /* arg or var index + constant pool index */
  74. #define JOF_JUMPX         9       /* signed 32-bit jump offset immediate */
  75. #define JOF_TABLESWITCHX  10      /* extended (32-bit offset) table switch */
  76. #define JOF_LOOKUPSWITCHX 11      /* extended (32-bit offset) lookup switch */
  77. #define JOF_UINT24        12      /* extended unsigned 24-bit literal (index) */
  78. #define JOF_LITOPX        13      /* JOF_UINT24 followed by op being extended,
  79.                                      where op if JOF_CONST has no unsigned 16-
  80.                                      bit immediate operand */
  81. #define JOF_TYPEMASK      0x000f  /* mask for above immediate types */
  82. #define JOF_NAME          0x0010  /* name operation */
  83. #define JOF_PROP          0x0020  /* obj.prop operation */
  84. #define JOF_ELEM          0x0030  /* obj[index] operation */
  85. #define JOF_MODEMASK      0x0030  /* mask for above addressing modes */
  86. #define JOF_SET           0x0040  /* set (i.e., assignment) operation */
  87. #define JOF_DEL           0x0080  /* delete operation */
  88. #define JOF_DEC           0x0100  /* decrement (--, not ++) opcode */
  89. #define JOF_INC           0x0200  /* increment (++, not --) opcode */
  90. #define JOF_INCDEC        0x0300  /* increment or decrement opcode */
  91. #define JOF_POST          0x0400  /* postorder increment or decrement */
  92. #define JOF_IMPORT        0x0800  /* import property op */
  93. #define JOF_FOR           0x1000  /* for-in property op */
  94. #define JOF_ASSIGNING     JOF_SET /* hint for JSClass.resolve, used for ops
  95.                                      that do simplex assignment */
  96. #define JOF_DETECTING     0x2000  /* object detection flag for JSNewResolveOp */
  97. #define JOF_BACKPATCH     0x4000  /* backpatch placeholder during codegen */
  98. #define JOF_LEFTASSOC     0x8000  /* left-associative operator */
  99. #define JOF_DECLARING    0x10000  /* var, const, or function declaration op */
  100. #define JOF_XMLNAME      0x20000  /* XML name: *, a::b, @a, @a::b, etc. */
  101.  
  102. #define JOF_TYPE_IS_EXTENDED_JUMP(t) \
  103.     ((unsigned)((t) - JOF_JUMPX) <= (unsigned)(JOF_LOOKUPSWITCHX - JOF_JUMPX))
  104.  
  105. /*
  106.  * Immediate operand getters, setters, and bounds.
  107.  */
  108.  
  109. /* Short (2-byte signed offset) relative jump macros. */
  110. #define JUMP_OFFSET_LEN         2
  111. #define JUMP_OFFSET_HI(off)     ((jsbytecode)((off) >> 8))
  112. #define JUMP_OFFSET_LO(off)     ((jsbytecode)(off))
  113. #define GET_JUMP_OFFSET(pc)     ((int16)(((pc)[1] << 8) | (pc)[2]))
  114. #define SET_JUMP_OFFSET(pc,off) ((pc)[1] = JUMP_OFFSET_HI(off),               \
  115.                                  (pc)[2] = JUMP_OFFSET_LO(off))
  116. #define JUMP_OFFSET_MIN         ((int16)0x8000)
  117. #define JUMP_OFFSET_MAX         ((int16)0x7fff)
  118.  
  119. /*
  120.  * When a short jump won't hold a relative offset, its 2-byte immediate offset
  121.  * operand is an unsigned index of a span-dependency record, maintained until
  122.  * code generation finishes -- after which some (but we hope not nearly all)
  123.  * span-dependent jumps must be extended (see OptimizeSpanDeps in jsemit.c).
  124.  *
  125.  * If the span-dependency record index overflows SPANDEP_INDEX_MAX, the jump
  126.  * offset will contain SPANDEP_INDEX_HUGE, indicating that the record must be
  127.  * found (via binary search) by its "before span-dependency optimization" pc
  128.  * offset (from script main entry point).
  129.  */
  130. #define GET_SPANDEP_INDEX(pc)   ((uint16)(((pc)[1] << 8) | (pc)[2]))
  131. #define SET_SPANDEP_INDEX(pc,i) ((pc)[1] = JUMP_OFFSET_HI(i),                 \
  132.                                  (pc)[2] = JUMP_OFFSET_LO(i))
  133. #define SPANDEP_INDEX_MAX       ((uint16)0xfffe)
  134. #define SPANDEP_INDEX_HUGE      ((uint16)0xffff)
  135.  
  136. /* Ultimately, if short jumps won't do, emit long (4-byte signed) offsets. */
  137. #define JUMPX_OFFSET_LEN        4
  138. #define JUMPX_OFFSET_B3(off)    ((jsbytecode)((off) >> 24))
  139. #define JUMPX_OFFSET_B2(off)    ((jsbytecode)((off) >> 16))
  140. #define JUMPX_OFFSET_B1(off)    ((jsbytecode)((off) >> 8))
  141. #define JUMPX_OFFSET_B0(off)    ((jsbytecode)(off))
  142. #define GET_JUMPX_OFFSET(pc)    ((int32)(((pc)[1] << 24) | ((pc)[2] << 16)    \
  143.                                          | ((pc)[3] << 8) | (pc)[4]))
  144. #define SET_JUMPX_OFFSET(pc,off)((pc)[1] = JUMPX_OFFSET_B3(off),              \
  145.                                  (pc)[2] = JUMPX_OFFSET_B2(off),              \
  146.                                  (pc)[3] = JUMPX_OFFSET_B1(off),              \
  147.                                  (pc)[4] = JUMPX_OFFSET_B0(off))
  148. #define JUMPX_OFFSET_MIN        ((int32)0x80000000)
  149. #define JUMPX_OFFSET_MAX        ((int32)0x7fffffff)
  150.  
  151. /*
  152.  * A literal is indexed by a per-script atom map.  Most scripts have relatively
  153.  * few literals, so the standard JOF_CONST format specifies a fixed 16 bits of
  154.  * immediate operand index.  A script with more than 64K literals must push all
  155.  * high-indexed literals on the stack using JSOP_LITERAL, then use JOF_ELEM ops
  156.  * instead of JOF_PROP, etc.
  157.  */
  158. #define ATOM_INDEX_LEN          2
  159. #define ATOM_INDEX_HI(i)        ((jsbytecode)((i) >> 8))
  160. #define ATOM_INDEX_LO(i)        ((jsbytecode)(i))
  161. #define GET_ATOM_INDEX(pc)      ((jsatomid)(((pc)[1] << 8) | (pc)[2]))
  162. #define SET_ATOM_INDEX(pc,i)    ((pc)[1] = ATOM_INDEX_HI(i),                  \
  163.                                  (pc)[2] = ATOM_INDEX_LO(i))
  164. #define GET_ATOM(cx,script,pc)  js_GetAtom((cx), &(script)->atomMap,          \
  165.                                            GET_ATOM_INDEX(pc))
  166.  
  167. /* A full atom index for JSOP_LITERAL uses 24 bits of immediate operand. */
  168. #define LITERAL_INDEX_LEN       3
  169. #define LITERAL_INDEX_HI(i)     ((jsbytecode)((i) >> 16))
  170. #define LITERAL_INDEX_MID(i)    ((jsbytecode)((i) >> 8))
  171. #define LITERAL_INDEX_LO(i)     ((jsbytecode)(i))
  172. #define GET_LITERAL_INDEX(pc)   ((jsatomid)(((pc)[1] << 16) |                 \
  173.                                             ((pc)[2] << 8) |                  \
  174.                                             (pc)[3]))
  175. #define SET_LITERAL_INDEX(pc,i) ((pc)[1] = LITERAL_INDEX_HI(i),               \
  176.                                  (pc)[2] = LITERAL_INDEX_MID(i),              \
  177.                                  (pc)[3] = LITERAL_INDEX_LO(i))
  178.  
  179. /* Atom index limit is determined by SN_3BYTE_OFFSET_FLAG, see jsemit.h. */
  180. #define ATOM_INDEX_LIMIT_LOG2   23
  181. #define ATOM_INDEX_LIMIT        ((uint32)1 << ATOM_INDEX_LIMIT_LOG2)
  182.  
  183. /* Actual argument count operand format helpers. */
  184. #define ARGC_HI(argc)           ((jsbytecode)((argc) >> 8))
  185. #define ARGC_LO(argc)           ((jsbytecode)(argc))
  186. #define GET_ARGC(pc)            ((uintN)(((pc)[1] << 8) | (pc)[2]))
  187. #define ARGC_LIMIT              ((uint32)1 << 16)
  188.  
  189. /* Synonyms for quick JOF_QARG and JOF_QVAR bytecodes. */
  190. #define GET_ARGNO(pc)           GET_ARGC(pc)
  191. #define SET_ARGNO(pc,argno)     SET_JUMP_OFFSET(pc,argno)
  192. #define ARGNO_LEN               JUMP_OFFSET_LEN
  193. #define GET_VARNO(pc)           GET_ARGC(pc)
  194. #define SET_VARNO(pc,varno)     SET_JUMP_OFFSET(pc,varno)
  195. #define VARNO_LEN               JUMP_OFFSET_LEN
  196.  
  197. struct JSCodeSpec {
  198.     const char          *name;          /* JS bytecode name */
  199.     const char          *token;         /* JS source literal or null */
  200.     int8                length;         /* length including opcode byte */
  201.     int8                nuses;          /* arity, -1 if variadic */
  202.     int8                ndefs;          /* number of stack results */
  203.     uint8               prec;           /* operator precedence */
  204.     uint32              format;         /* immediate operand format */
  205. };
  206.  
  207. extern const char       js_const_str[];
  208. extern const char       js_var_str[];
  209. extern const char       js_function_str[];
  210. extern const char       js_in_str[];
  211. extern const char       js_instanceof_str[];
  212. extern const char       js_new_str[];
  213. extern const char       js_delete_str[];
  214. extern const char       js_typeof_str[];
  215. extern const char       js_void_str[];
  216. extern const char       js_null_str[];
  217. extern const char       js_this_str[];
  218. extern const char       js_false_str[];
  219. extern const char       js_true_str[];
  220. extern const char       js_default_str[];
  221. extern const JSCodeSpec js_CodeSpec[];
  222. extern uintN            js_NumCodeSpecs;
  223. extern const jschar     js_EscapeMap[];
  224.  
  225. /*
  226.  * Return a GC'ed string containing the chars in str, with any non-printing
  227.  * chars or quotes (' or " as specified by the quote argument) escaped, and
  228.  * with the quote character at the beginning and end of the result string.
  229.  */
  230. extern JSString *
  231. js_QuoteString(JSContext *cx, JSString *str, jschar quote);
  232.  
  233. /*
  234.  * JSPrinter operations, for printf style message formatting.  The return
  235.  * value from js_GetPrinterOutput() is the printer's cumulative output, in
  236.  * a GC'ed string.
  237.  */
  238. extern JSPrinter *
  239. js_NewPrinter(JSContext *cx, const char *name, uintN indent, JSBool pretty);
  240.  
  241. extern void
  242. js_DestroyPrinter(JSPrinter *jp);
  243.  
  244. extern JSString *
  245. js_GetPrinterOutput(JSPrinter *jp);
  246.  
  247. extern int
  248. js_printf(JSPrinter *jp, const char *format, ...);
  249.  
  250. extern JSBool
  251. js_puts(JSPrinter *jp, const char *s);
  252.  
  253. #ifdef DEBUG
  254. /*
  255.  * Disassemblers, for debugging only.
  256.  */
  257. #include <stdio.h>
  258.  
  259. extern JS_FRIEND_API(JSBool)
  260. js_Disassemble(JSContext *cx, JSScript *script, JSBool lines, FILE *fp);
  261.  
  262. extern JS_FRIEND_API(uintN)
  263. js_Disassemble1(JSContext *cx, JSScript *script, jsbytecode *pc, uintN loc,
  264.                 JSBool lines, FILE *fp);
  265. #endif /* DEBUG */
  266.  
  267. /*
  268.  * Decompilers, for script, function, and expression pretty-printing.
  269.  */
  270. extern JSBool
  271. js_DecompileCode(JSPrinter *jp, JSScript *script, jsbytecode *pc, uintN len);
  272.  
  273. extern JSBool
  274. js_DecompileScript(JSPrinter *jp, JSScript *script);
  275.  
  276. extern JSBool
  277. js_DecompileFunctionBody(JSPrinter *jp, JSFunction *fun);
  278.  
  279. extern JSBool
  280. js_DecompileFunction(JSPrinter *jp, JSFunction *fun);
  281.  
  282. /*
  283.  * Find the source expression that resulted in v, and return a new string
  284.  * containing it.  Fall back on v's string conversion (fallback) if we can't
  285.  * find the bytecode that generated and pushed v on the operand stack.
  286.  *
  287.  * Search the current stack frame if spindex is JSDVG_SEARCH_STACK.  Don't
  288.  * look for v on the stack if spindex is JSDVG_IGNORE_STACK.  Otherwise,
  289.  * spindex is the negative index of v, measured from cx->fp->sp, or from a
  290.  * lower frame's sp if cx->fp is native.
  291.  */
  292. extern JSString *
  293. js_DecompileValueGenerator(JSContext *cx, intN spindex, jsval v,
  294.                            JSString *fallback);
  295.  
  296. #define JSDVG_IGNORE_STACK      0
  297. #define JSDVG_SEARCH_STACK      1
  298.  
  299. JS_END_EXTERN_C
  300.  
  301. #endif /* jsopcode_h___ */
  302.